/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package br.com.controller;

import br.com.dao.LoginDAO;
import br.com.modelo.LoginBean;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Felipe
 */
@WebServlet(name = "LoginController", urlPatterns = {"/LoginController"})
public class LoginController extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
          String login = request.getParameter("login");
          String senha = request.getParameter("senha");
                   
         Connection conexao = null;
         PreparedStatement stmt = null;
         ResultSet res = null;

         try{

              Class.forName("com.mysql.jdbc.Driver");
              conexao = DriverManager.getConnection("jdbc:mysql://localhost/hotel","root","admin");
              stmt = conexao.prepareStatement("SELECT * FROM func WHERE login=?");
              stmt.setString(1, login);
              res = stmt.executeQuery();
             LoginBean usuario = null;
              if(res.first()){
                   usuario = new LoginBean();
                  usuario.setLogin(res.getString("login"));
                   usuario.setSenha(res.getString("senha"));
              }
              if(usuario==null||!usuario.getSenha().equals(senha)){
                    response.sendRedirect("login.jsp");
                    

              }else{

                  

                   response.sendRedirect("index.jsp");

              }

         }catch(Exception e){

              throw new ServletException(e);

         }finally{

              try{

                   res.close();

                   stmt.close();

                   conexao.close();

              }catch(Exception e){

                   e.printStackTrace();

              }



         }
    
}
}
